
import 'package:baselib/utils/keyboard_util.dart';
import 'package:baselib/utils/load_util.dart';
import 'package:baselib/utils/log_util.dart';
import 'package:baselib/utils/md5_util.dart';
import 'package:baselib/utils/navigator_util.dart';
import 'package:baselib/utils/toast_util.dart';
import 'package:baselib/widgets/refresh_util.dart';
import 'package:framelib/base/base_view_model.dart';
import 'package:framelib/config/api_config.dart';
import 'package:framelib/config/route_config.dart';
import 'package:framelib/net/baseentity.dart';
import 'package:framelib/utils/nim_sdk_util.dart';
import 'package:framelib/utils/string_util.dart';
import 'package:framelib/widgets/page_widget.dart';
import 'package:get/state_manager.dart';
import 'package:get/get.dart';
import 'package:module_business/module_comm/model/user_entity.dart';
import 'package:module_business/module_comm/model/user_params_model.dart';
import 'package:module_business/module_comm/service/module_comm_service.dart';
import 'package:module_business/module_index/model/index_room_item_entity.dart';
import 'package:module_business/module_index/model/voice_chat_tag_entity.dart';
import 'package:module_business/module_index/service/module_index_service.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

class IndexTagViewModel extends BaseViewModel<ModuleIndexService>{

  ModuleIndexService moduleIndexService;
  RefreshController refreshController = RefreshController(initialRefresh: true);
  RefreshState loadStatus=RefreshState.OK;
  int page = 1;
  bool isCanMoreData = false;
  List<IndexRoomItemEntity> dataLists=[];

  VoiceChatTagEntity voiceChatTag;
  IndexTagViewModel({this.voiceChatTag});

  @override
  ModuleIndexService createService() =>moduleIndexService = ModuleIndexService();

  @override
  void onInit() {
    super.onInit();
  }

  onRefresh({int pageCur,}) async{
    if(pageCur!=null)page=pageCur;
    Map<String,dynamic> upData={
      'searchValue':voiceChatTag?.id,
    };
    BaseEntity baseEntity= await moduleIndexService
        .api_voicechat_room_findRoomListByTag(parmsMap: upData);
    if(refreshController!=null)refreshController.refreshCompleted();
    if(refreshController!=null)refreshController.loadComplete();
    if(baseEntity?.code==ApiConfig.SUCCESS_CODE){
      List<IndexRoomItemEntity> dataTemp=baseEntity?.result??[];
      if(page==1){
        dataLists=dataTemp;
      }else{
        dataLists.addAll(dataTemp);
      }
      LogUtil.d('onRefresh-->dataLists=${dataLists.map((e) => e.toString()).toList()}');
      if(((dataTemp?.length??0)<10)){
        isCanMoreData=false;
      }else{
        isCanMoreData=true;
      }
      //计算是否为空
      if(((dataLists?.length??0)==0)){
        loadStatus=RefreshState.EMPTY;
      }else{
        //计算被选中的
        loadStatus=RefreshState.OK;
      }
    }else{
      loadStatus=RefreshState.NET_ERROR;
      ToastUtil.toast(baseEntity?.message);
    }
    update();
  }

  void toRoom(int index) async{
    //api_voicechat_room_joinRoom
    var dataItem = dataLists[index];
    LoadUtil.showLoad(loadingText: StringUtil.instance.data_loading);
    Map<String,dynamic> upData={
      'id': dataItem?.id,
      'password': dataItem?.password,
    };
    LogUtil.d('toMyRoom--->upData=$upData');
    BaseEntity baseEntity = await moduleIndexService.api_voicechat_room_joinRoom(parmsMap: upData);
    if(baseEntity?.code == ApiConfig.SUCCESS_CODE){
      IndexRoomItemEntity roomItem = baseEntity?.result;
      //云信进入房间
      var  enterChatroomResult = await NIMSDKUtil.instance.enterChatroom(
          roomId: roomItem?.yxRoomId,
          nickname: UserParamsModel.instance.selfNIMUser.nick,
          avatar: UserParamsModel.instance.selfNIMUser.avatar
      );
      LoadUtil.hideLoad();
      var enterChatroomResultMap = enterChatroomResult?.toMap();
      var enterChatroomResultData = enterChatroomResult?.data;
      LogUtil.d('进入聊天室------>enterChatroomResultMap=${enterChatroomResultMap}');
      if (enterChatroomResult.isSuccess) {
        LogUtil.d('进入聊天室成功');
        NavigatorUtil.pushByName(RouteConfig.myRoomPage,arguments: {
          'roomItem': roomItem
        });
        // joinRoomSuccess();
      }else{
        LogUtil.d('进入聊天室失败');
      }
    }else{
      LoadUtil.hideLoad();
      ToastUtil.toast(baseEntity?.message);
    }
  }

  void joinRoomSuccess() async{
    ///api/voicechat/room/joinRoomSuccess
  }
}
